username = document.querySelector('.username'),
    email = document.querySelector('.email'),
    mobile = document.querySelector('.mobile'),
    gender_radios = document.querySelectorAll('.gender'),
    id = location.href.split('?')[1].split('=')[1]

const select_oneuser = async() => {

    let response = await ajax(`select_oneuser?id=${id}`)
    let userinfo = response.data[0]
    username.value = userinfo.username
    email.value = userinfo.email
    mobile.value = userinfo.mobile

    for (const radio of gender_radios) {
        if (radio.value == userinfo.gender) {
            radio.checked = true
        }
    }
}

const updateuser = async() => {
    let email_new = email.value,
        mobile_new = mobile.value,
        gender
    for (const radio of gender_radios) {
        if (radio.checked) {
            gender = radio.value
        }
    }
    try {
        let obj = { email: email_new, mobile: mobile_new, gender, id }
        const response = await ajax('updateuser', 'PUT', obj)
        alert(response.meta.msg)
        location.href = './users.html'
    } catch (error) {

    }
}

window.onload = function() {
    select_oneuser()
}